<?php


namespace Littlexiaoshuishui\Upay;


use Littlexiaoshuishui\Upay\Abstracts\GatewayInterface;
use Littlexiaoshuishui\Upay\Gateways\Qfpay\Support;
use ML\Pay\PayException;

class Api
{
    protected $config;

    public function __call($method,$param=null)
    {
        $gateway = get_class($this).'\\'.Support::namePase($method).'Gateway';
        if (class_exists($gateway)) {
            if (!empty($param)) {
                $this->config = array_merge($this->config,$param[0]);
            }
            $app = new $gateway($this->config);
            if ($app instanceof GatewayInterface) {
                return $app;
            }
        }
        throw new PayException("gateway no found :".$method);
    }

    public function _proxy($param,$type,$method)
    {
        $gateway = get_class($this).'\\'. Support::namePase($type).'Gateway';

        if (!class_exists($gateway) || !is_callable([new $gateway(), $method])) {
            throw new PayException(PayException::INVALID_PARAMS,"{$gateway} Done Not Exist Or Done Not Has FIND Method");
        }

        return call_user_func([new $gateway($this->config), $method], $param);
    }

}